package com.example.webdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Date 2021/6/24 13:31
 * @Created by wangle
 * @Description  文件上传测试
 */
@Slf4j
@Controller
public class formcontroller {

    @GetMapping("/form_layouts")
    public String formlayouts(){



        return  "form/form_layouts";
    }
    @PostMapping("/upload")
    public   String upload1(@RequestParam("email") String email
            ,@RequestParam("username") String username,
            @RequestPart("headerimage")MultipartFile heaerimage
    ,@RequestPart("photos") MultipartFile [] photos) throws IOException {

        log.info("email={},username={},hederimage={},photos={}",email,username,
                heaerimage.getSize(),photos.length);

        if(!heaerimage.isEmpty()){
           String s= heaerimage.getOriginalFilename();
            heaerimage.transferTo(new File("D:\\aa\\"+s));

        }

        if(photos.length>0){

            for (MultipartFile f:photos
                 ) {

                if (!f.isEmpty()) {
                    String s= f.getOriginalFilename();
                    f.transferTo(new File("D:\\aa\\"+s));

                }


            }
        }

        return  "main";
    }

}
